package jin.si.service.impl;

import com.google.gson.Gson;
import jin.si.dao.HomePageMapper;
import jin.si.pojo.*;
import jin.si.service.HomePageService;
import jin.si.util.SensitiveWordsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import redis.clients.jedis.Jedis;
import sun.security.provider.MD5;

import java.util.List;

/**
 * ClassName:HomePage
 * Package:jin.si.dao
 * Description:
 *
 * @Date:2022/3/4 9:53
 * @Author: XJR
 */
@Service
public class HomePageServiceImple implements HomePageService {

    @Autowired
    private HomePageMapper mapper;

    @Qualifier
    private  Jedis jedis =new Jedis("192.168.188.100",6379);

    //查询轮播图
    public List<JsCarousel> selectJsCarousel(Integer display){
        List<JsCarousel> jsCarousels = mapper.selectJsCarousel(display);
        for (JsCarousel js:jsCarousels) {
            String imgAddress = js.getImgAddress();
            js.setImgAddress("http://47.94.11.84/" + imgAddress);
        }
        return jsCarousels;
    };

    //查询课程
    public List<JsCourse> selectJsCourse(Integer courseStatus,Integer orderStatus,Integer orderType){
        List<JsCourse> jsCourses = mapper.selectJsCourse(courseStatus,orderStatus,orderType);
        for (JsCourse js:jsCourses){
            String url = js.getUrl();
            js.setUrl("http://47.94.11.84/"+url);
        }
        Gson gson = new Gson();
        String selectJsCourse = jedis.set("selectJsCourse", gson.toJson(jsCourses));
        return jsCourses;
    };

    //通过id查看课程详情
    public JsCourse selectJsCourseById(Integer courseId){
        JsCourse jsCourse = mapper.selectJsCourseById(courseId);
        String url = jsCourse.getUrl();
        jsCourse.setUrl("http://47.94.11.84/"+url);

        String courseUrl = jsCourse.getCourseUrl();
        jsCourse.setCourseUrl("http://47.94.11.84/"+courseUrl);

        String avatarUrl = jsCourse.getAvatarUrl();
        jsCourse.setAvatarUrl("http://47.94.11.84/"+avatarUrl);
        Integer recommend = jsCourse.getRecommend();
        if(recommend==2){
            jsCourse.setRecommendd("新品");
        }else if(recommend==3){
            jsCourse.setRecommendd("推荐");
        }else{
            jsCourse.setRecommendd("其他");
        }
        return jsCourse;
    };

    //查看课程分类
    public List<JsCourse> selectJsCourseTypeById(Integer id,Integer courseStatus){
        List<JsCourse> jsCourse = mapper.selectJsCourseTypeById(id, courseStatus);
        return jsCourse;
    };

    //查看课程评论
    public List<JsCourse> selectJsCourseComment(JsCommentLike jsCommentLike){
        List<JsCourse> jsCourses = mapper.selectJsCourseComment(jsCommentLike);
        for (JsCourse js:jsCourses){
            String avatarUrl = js.getAvatarUrl();
            js.setAvatarUrl("http://47.94.11.84/"+avatarUrl);
        }
        List<JsCourse> listFilterr = SensitiveWordsUtil.getListFilterr(jsCourses);
        return listFilterr;
    };

    //查看课程销量
    public Integer selectJsCourseSalesVolume(Integer orderStatus,Integer productId,Integer userId){
        Integer selectJsCourseSalesVolume = mapper.selectJsCourseSalesVolume(orderStatus, productId,userId);
        return selectJsCourseSalesVolume;
    };
    //查看课程点赞数
    public Integer selectJsCourseLikeNum(Integer type,Integer courseId,Integer userId){
        Integer selectJsCourseLikeNum = mapper.selectJsCourseLikeNum(type, courseId,userId);
        return selectJsCourseLikeNum;
    };
    //查看课程收藏量
    public Integer selectJsCourseCollectNum(Integer type,Integer courseId,Integer userId){
        Integer selectJsCourseCollectNum = mapper.selectJsCourseCollectNum(type, courseId,userId);
        return selectJsCourseCollectNum;
    };
    //查看课程分享数量
    public Integer selectJsCourseShareNum(Integer type,Integer courseId){
        Integer selectJsCourseShareNum = mapper.selectJsCourseShareNum(type, courseId);
        return selectJsCourseShareNum;
    };
    //添加课程点赞量
    public Boolean insertCourseLikeNum(JsFabulous jsFabulous){
        Boolean insertCourseLikeNum = mapper.insertCourseLikeNum(jsFabulous);
        return insertCourseLikeNum;
    };
    //删除课程点赞量
    public Boolean delCourseLikeNum(JsFabulous jsFabulous){
        Boolean delCourseLikeNum = mapper.delCourseLikeNum(jsFabulous);
        return delCourseLikeNum;
    };
    //添加课程收藏量
    public Boolean insertCourseCollectNum(JsUserCollection jsUserCollection){
        Boolean insertCourseCollectNum = mapper.insertCourseCollectNum(jsUserCollection);
        return insertCourseCollectNum;
    };
    //删除课程收藏量
    public Boolean delCourseCollectNum(JsUserCollection jsUserCollection){
        Boolean delCourseCollectNum = mapper.delCourseCollectNum(jsUserCollection);
        return delCourseCollectNum;
    };
    //添加课程评论
    public Boolean insertCourseComment(JsComment comment){
        Boolean insertCourseComment = mapper.insertCourseComment(comment);
        return insertCourseComment;
    };
    //添加课程评论的点击量
    public Boolean insertCourseCommentLikeNum(JsCommentLike jsCommentLike){
        Boolean insertCourseCommentLikeNum = mapper.insertCourseCommentLikeNum(jsCommentLike);
        return insertCourseCommentLikeNum;
    };
    //删除课程评论的点击量
    public Boolean deleteCourseCommentLikeNum(JsCommentLike jsCommentLike){
        Boolean deleteCourseCommentLikeNum = mapper.deleteCourseCommentLikeNum(jsCommentLike);
        return deleteCourseCommentLikeNum;
    };
    //查看当前用户是否点赞课程评论
    public JsCommentLike selectCourseCommentLikeNum(Integer productId,Integer userId,Integer userType){
        JsCommentLike jsCommentLike = mapper.selectCourseCommentLikeNum(productId, userId,userType);
        return jsCommentLike;
    };

}
